package com.hongdee.poi;

import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;

import java.io.InputStream;

/**
 * 名  称:PoiExcelUtil
 * 类  型: class
 * 描  述: 用一句话描述类的作用
 * 路  径:com.hongdee.common.util
 * 作  者:My
 * 日  期:2017/2/16 10:21
 */
public class PoiExcelUtil {

    public static boolean readExcel(String type,InputStream inputStream, int sheetIndex, XSSFSheetXMLHandler.SheetContentsHandler sheetContentsHandler, ExcelSheetCallback sheetCallback)
    {


        //日期格式解析不统一 20161227
        if (type.endsWith(".xlsx"))
        {
            return readXlsxFile(inputStream, sheetIndex, sheetContentsHandler, sheetCallback);
        }
        return readExcelFile(inputStream, sheetIndex, sheetContentsHandler, sheetCallback);
    }

    public static boolean readXlsxFile(InputStream inputStream, int sheetIndex, XSSFSheetXMLHandler.SheetContentsHandler sheetContentsHandler, ExcelSheetCallback sheetCallback)
    {
        try
        {
            XLSXReader reader = new XLSXReader(inputStream, sheetContentsHandler, sheetCallback);
            if (sheetIndex > -1)
                reader.process(sheetIndex);
            else
                reader.process();
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean readExcelFile(InputStream inputStream, int sheetIndex, XSSFSheetXMLHandler.SheetContentsHandler sheetContentsHandler, ExcelSheetCallback sheetCallback)
    {
        try
        {
            ExcelReader reader = new ExcelReader(inputStream, sheetContentsHandler, sheetCallback);
            if (sheetIndex > -1)
                reader.process(sheetIndex);
            else
                reader.process();
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
